# -*- encoding : utf-8 -*-
import feedparser
from datetime import datetime
from pprint import pprint
import re
import os, sys

MAX_FETCH = 10
image_pat = re.compile(ur'<img\s+src="([^"]*)"', re.M)


# setup for Django ORM
sys.path.append( r'I:\django\projects\snoweblog')
#sys.path.append( r'F:\focus\2006-08-29\snoweblog')
os.environ['DJANGO_SETTINGS_MODULE']='settings'
from mashup.models import Article, Source, Photo

def get_entries(rss):
    source = rss.split('//')[1].split('/')[0]
    d = feedparser.parse(rss) 
    if len(d.entries) > MAX_FETCH:
        d.entries = d.entries[:MAX_FETCH]
    return source, d.entries

def to_datetime(date_parsed):
    return datetime(*date_parsed[:6])

def get_description(entry):
    if not hasattr(entry,'description'):
        return ''
    return entry.description

def parse_article(rss):  
    source, entries = get_entries(rss)    
    return [ (dict(name=source), dict(title = entry.title, description=get_description(entry), 
                           link=entry.link, pub_date=to_datetime(entry.date_parsed))) 
             for entry in entries ]

def extract_link(description):
    m = image_pat.search(description)
    if m is None:
        return ''
    return m.group(1)

def parse_photo(rss):
    source, entries = get_entries(rss)
    return [ (dict(name=source), dict(title = entry.title,
            link=extract_link(entry.description), pub_date=to_datetime(entry.date_parsed)))
                            for entry in entries ]

def get_or_create(lookup_dict, create_dict, manager=Source.objects):
    # differ from stardard get_or_create: using an extra lookup dict
    try:
        s = manager.get(**lookup_dict)
    # FIXME dont know how to throw exception like Person.DoesNotExist
    except :
        s = manager.create(**create_dict)       
    return s

def fill_db_article(rss):
    for src, article in parse_article(rss):
        s = get_or_create(src, src)
        get_or_create(dict(link=article['link']), article,
                                manager=s.article_set)      
def fill_db_photo(rss):
    for src, photo in parse_photo(rss):
        s = get_or_create(src, src)
        get_or_create(dict(link=photo['link']), photo,
                                manager=s.photo_set)   

if __name__ == '__main__':
    article_rss = [
            'http://www.digg.com/rss/containertechnology.xml',
            'http://del.icio.us/rss/popular', 
            'http://www.boingboing.net/index.rdf', 
    ]
    photo_rss = [
            'http://api.flickr.com/services/feeds/photos_public.gne?tags=nature&format=rss_200',
    ]

    [fill_db_article(rss) for rss in article_rss]
    [fill_db_photo(rss) for rss in photo_rss]



